package com.sam.im.heiqun.uis.activities;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.constant.Constant;
import com.sam.im.heiqun.entities.ImFriendEntivity;
import com.sam.im.heiqun.entities.ValidateEntivity;
import com.sam.im.heiqun.nets.PGService;
import com.yuyh.library.utils.EventBusType;
import com.sam.im.heiqun.utils.EventBusTypeObject;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.yuyh.library.nets.callbacks.AbsAPICallback;
import com.yuyh.library.nets.exceptions.ApiException;
import com.yuyh.library.uis.activitys.BaseActivity;
import com.yuyh.library.utils.toast.ToastUtils;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Joe on 2017/1/13.
 */

public class AlterRemarkActivity extends BaseActivity {
    @BindView(R.id.center_title)
    TextView title;
    @BindView(R.id.right_commit)
    TextView commit;
    @BindView(R.id.alter_edit)
    EditText alterEdit;
    @BindView(R.id.left_back)
    ImageView back;

    public static int ALTER_REMARK = 10;

    PGService mPgService;

    boolean isNick;
    private String destid;
    private String remark ;
    @Override
    public int getContentViewId() {
        return R.layout.activity_alter_nick;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        mPgService = PGService.getInstance();
        title.setText(R.string.alter_remark);
        alterEdit.setHint(R.string.please_input_remark);
        destid = getIntent().getStringExtra("destid");
        remark = getIntent().getStringExtra("remark");
        if (remark != null&&!"".equals(remark)) {
            alterEdit.setText(remark);
        }
    }

    @OnClick({R.id.left_back, R.id.right_commit})
    public void onClick(View view) {

        Intent data = new Intent();
        switch (view.getId()) {
            case R.id.right_commit:
                //做提交
                String str = alterEdit.getText().toString().trim();
                if (str!=null&&!"".equals(str)){
                    updateRemark(destid, str);
                }else {
                    showToast(getResources().getString(R.string.bzm_nonull));
                }

                break;
            case R.id.left_back:
                finish();
                break;
        }
    }

    /**
     * 修改备注
     *
     * @param uId
     */
    public void updateRemark(String uId, final String nick) {
        mPgService.updateRemark(uId, ToolsUtils.getMyUserId(), nick).subscribe(new AbsAPICallback<ValidateEntivity>() {
            @Override
            protected void onResultError(ApiException ex) {
                new ToastUtils().showSingleToast(ex.getDisplayMessage());
            }

            @Override
            public void onNext(ValidateEntivity data) {
                new ToastUtils().showSingleToast(data.getInfo());

                ImFriendEntivity imFriendEntivity = ToolsUtils.getMyFriendForId(Long.parseLong(destid));
                imFriendEntivity.setRemark(nick);
                imFriendEntivity.save();

                ImFriendEntivity byId = ImFriendEntivity.findById(ImFriendEntivity.class, Long.valueOf(destid));
                byId.setRemark(nick);
                ImFriendEntivity.save(byId);

                EventBus.getDefault().post(imFriendEntivity);
//                EventBus.getDefault().post(Constant.MAIN_BOTTOM_TIP_NUM);//通知MessageFragment刷新界面
                EventBus.getDefault().post(new EventBusTypeObject(EventBusType.REFRESH_TYPE_ONE,destid,""+Constant.FROM_TYPE_MAM));//通知MessageFragment刷新界面
                EventBus.getDefault().post(Constant.BLACK_BROADCAST_FOUND_REFRESH);
                EventBus.getDefault().post("点赞");//刷新朋友圈列表，使用备注名

                finish();
            }
        });
    }
}
